<?php
include '../../../wp-blog-header.php';

$brand_id = '1106';
$url = 'http://www.vamoosevacations.com/rss/catalog/category/cid/69/store_id/1/';
$count = 0;

function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$vamoose_deals = xml2array($curl_json_output);
//shuffle($vamoose_deals['coupon:deals']['coupon:deal']);
//$vamoose_deals = array_slice($vamoose_deals['coupon:deals']['coupon:deal'], 0, 3);

//echo '<pre>';
//print_r($vamoose_deals['rss']['channel']['item']);
//echo '</pre>';
//exit();

foreach($vamoose_deals['rss']['channel']['item'] as $key => $value){
    
    $columns_deal['name'] = pods_sanitize($value['title']);
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    $ext_img_url = $value['image']['url'];
    $deal = new Pod('deal');
    $where = "t.camp_link = '".$columns_deal['camp_link']."'";
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    
    if($dealsFound == 0)
    {
	  $count++;
      unset($ids);
      unset($current_cities);
      $City = new Pod('city');
    	$City->findRecords('name ASC', -1);
    	while($City->fetchRecord())
    	{
      	$ids  = $City->get_field('id');
      	$current_cities[] = $ids;
    	}
      $current_cities = implode(',',$current_cities);
      $columns_deal['city'] = $current_cities;
      
      $shortened_url = make_bitly_url($value['link']);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($value['link']);
      }
      $columns_deal['extlink'] = $shortened_url;

      
      date_default_timezone_set('Asia/Kolkata');
      $columns_deal['camp_start'] = date('Y-m-d H:i:s'); //current time
	  $columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime('+10 day')); //current time
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $columns_deal['quickcamp'] = '1';
      $columns_deal['dealcheck'] = '1';
     // $columns_deal['oriprice'] = floor($value['coupon:value']);
     // $columns_deal['disper'] = $value['coupon:percent_discount'];
     // $columns_deal['disprice'] = floor($value['coupon:price']);
      $columns_deal['ext_image_url'] = $ext_img_url;
      $columns_deal['short'] = pods_sanitize(strip_tags($value['description']));
      
      $category = new Pod('category','c-travel-offers');            //set a fixed category
      $columns_deal['camp_cat'] = $category->get_field('id');
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
      
      
     echo '<pre>';
     print_r($params);
     echo '</pre>';
     // exit();
      
      $api->save_pod_item($params);
	  if($count == 1){
		exit();
	  }
  }

 
}
?>